//
//  PlayTracker.swift
//  learnSwift01
//
//  Created by zmj27404 on 16/4/17.
//  Copyright © 2016年 zmj27404. All rights reserved.
//

import Foundation
//结构体写游戏等级
struct LevelTracker {
    static var highestUnlockedLevel = 6
    static func unlockLevel (level: Int) {
        if level > highestUnlockedLevel {
            highestUnlockedLevel = level
        }
    }
    
    static func levelIsUnlocked(level: Int) -> Bool {
        return level <= highestUnlockedLevel
    }
    
    var currentLevel = 1
    //使用mutating是因为方法内部更改了结构体的成员变量
    mutating func advanceToLevel(level: Int) -> Bool {
        if LevelTracker.levelIsUnlocked(level) {
            currentLevel = level
            return true
        }
        else {
            return false
        }
    }
}

//类放游戏玩家，每个玩家都有游戏等级结构体
class Player {
    var tracker = LevelTracker()
    let playerName: String
    func completedLevel(level: Int) {
        LevelTracker.unlockLevel(level + 1)
        tracker.advanceToLevel(level)
    }
    init(name: String) {
        playerName = name
    }
}

